package com.example.administrator.myapplication.download;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.FragmentHotCommentCommBinding;
import com.example.administrator.myapplication.download.bean.hotentity.HotCommentEntity;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotCommentCommFragment extends Fragment implements Callback<HotCommentEntity> {
   private FragmentHotCommentCommBinding binding;
    private CommentRecyclerAdapter<Object> adapter;

    public static HotCommentCommFragment newInstance(String name,int  id) {
        Bundle args = new Bundle();
        args.putString("name",name);
        args.putInt("id",id);
        HotCommentCommFragment fragment = new HotCommentCommFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater,R.layout.fragment_hot_comment_comm, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.getBinding(view);
        aboutIntentExtra();
    }

    private void aboutIntentExtra() {
        String name = getArguments().getString("name");
        int id = getArguments().getInt("id");
        Log.d("1","name"+name+"id"+id);
        aboutRecycler(id);
    }

    private void aboutRecycler(int id) {
        switch (id) {
            case 1:
                aboutNewHot("hot");
                break;
            case 2:
                aboutNewHot("recent");
                break;
            case 3:
                aboutNewHot("classic");
                break;
        }
    }

    private void aboutNewHot(String calcDimension) {
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(), R.layout.fragment_hot_comment_comm_item, BR.hotItemEntity);
        binding.recycler.setAdapter(adapter);
        XmlyService service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getHotCommentEntity(calcDimension).enqueue(this);
    }

    @Override
    public void onResponse(Call<HotCommentEntity> call, Response<HotCommentEntity> response) {
        Log.d("1",response.body().getList().size()+"个");
        adapter.addAll(response.body().getList());
    }

    @Override
    public void onFailure(Call<HotCommentEntity> call, Throwable t) {
        Toast.makeText(getContext(), "最火界面"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
